﻿using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using ICSharpCode.SharpZipLib.Zip;

namespace mmCore.Common
{
    public class Utils
    {
        public static bool Download(string url, string outFile)
        {
            var client = new WebClient();
            client.DownloadFile(url, outFile);
            return true;
        }

        public static bool DownloadAndExtract(string url, string outDir)
        {
            if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(outDir))
            {
                return false;
            }

            var zipFile = Path.Combine(outDir, string.Format("subtitle.zip"));

            try
            {
                var client = new WebClient();
                client.DownloadFile(url, zipFile);
                var fz = new FastZip();
                fz.ExtractZip(zipFile, outDir, "");
                File.Delete(zipFile);
            }
            catch(Exception exc)
            {
                ExceptionManager.ReportError(exc);
                try
                {
                    if (File.Exists(zipFile))
                    {
                        File.Delete(zipFile);
                    }
                }
                catch
                {
                }
                
                return false;
            }
            
            return true;
        }

        public static void OpenWebPage(string web)
        {
            var proc = new Process
            {
                StartInfo =
                {
                    FileName = web,
                    UseShellExecute = true
                }
            };
            proc.Start();
        }
    }
}
